package com.example.studyktdemo.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.studyktdemo.bean.HomeBean
import com.google.gson.Gson
import okhttp3.*
import java.io.IOException
import java.net.HttpURLConnection

class HomeFragmentViewModel :ViewModel(){
    private val TAG = "HomeFragmentViewModel"
    private var homeMutableLiveData:MutableLiveData<HomeBean>? = MutableLiveData()

    private var nextPageMutableLiveData:MutableLiveData<HomeBean>? = MutableLiveData()

    fun getHomeMutableLiveData(): MutableLiveData<HomeBean>? {
        return homeMutableLiveData
    }

    fun getNextPageMutableLiveData():MutableLiveData<HomeBean>?{
        return nextPageMutableLiveData
    }

    fun queryBannerData(){
        var client = OkHttpClient()
        var request = Request.Builder().url("https://baobab.kaiyanapp.com/api/v2/feed?")
            .get().build()
        var call = client.newCall(request)

        call.enqueue(object: Callback {
            override fun onResponse(call: Call?, response: Response?) {
                var code = response?.code()
                Log.d(TAG,"code---> $code")
                if(code== HttpURLConnection.HTTP_OK){
                    var result = response?.body()?.string()
                    var data = Gson().fromJson(result, HomeBean::class.java)
                    var homeData = data.issueList[0]?.itemList
                    if (homeData != null) {
                        homeMutableLiveData?.postValue(data)
                    }else{
                        homeMutableLiveData?.postValue(null)
                    }
                    //Log.d(TAG,"homeData----> $homeData")
                }
            }
            override fun onFailure(call: Call?, e: IOException?) {
                Log.d(TAG,"e---> ${e.toString()}")
            }
        })
    }

    //首页加载更多和初始化加载
    fun loadMore(nextPageUrl:String) {
        var client = OkHttpClient()
        var request = Request.Builder().url(nextPageUrl)
            .get().build()
        var call = client.newCall(request)

        call.enqueue(object: Callback {
            override fun onResponse(call: Call?, response: Response?) {
                var code = response?.code()
                Log.d(TAG,"code---> $code")
                if(code== HttpURLConnection.HTTP_OK){
                    var result = response?.body()?.string()
                    var data = Gson().fromJson(result, HomeBean::class.java)
                    var homeData = data.issueList[0]?.itemList
                    if (homeData != null) {
                        nextPageMutableLiveData?.postValue(data)
                    }else{
                        nextPageMutableLiveData?.postValue(null)
                    }
                    //Log.d(TAG,"homeData----> $homeData")
                }
            }
            override fun onFailure(call: Call?, e: IOException?) {
                Log.d(TAG,"e---> ${e.toString()}")
            }
        })
    }
}